package com.shujia.MapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;

import java.io.IOException;

/**
 * 读取文件
 * 统计每个单词出现的数量
 */
public class Demo2WordCount {
    //  Mapper<LongWritable, Text, Text, LongWritable>
    // Mapper<输入Map的key的类型,输入Map的value的类型,Map输出的Key的类型,Map输出的Value的类型>
    // Map默认的inputformat是TextInputFormat
    // TextInputFormat:会将数据每一行的偏移量作为key，每一行作为value输入到Map端
    public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
        // 实现自己的Map逻辑

        /**
         * @param key：输入Map的key
         * @param value：输入Map的Value
         * @param context：MapReduce程序的上下文环境，Map端的输出可以通过context发送到Reduce端
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
            // 获取一行数据
            // hadoop,hive,hbase => hadoop,1  hive,1 hbase,1
            String line = value.toString();
            // 按照逗号分割
            String[] splits = line.split(",");
            for (String word : splits) {
                // 将结果发送到reduce端
                context.write(new Text(word), new LongWritable(1));
            }

        }
    }

    // Reducer<Text, LongWritable,Text, LongWritable>
    // Reducer<Map输出的Key的类型,Map输出的Value的类型,Reduce输出的Key的类型,Reduce输出的Value的类型>
    public static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
        /**
         * @param key：从Map端输出并经过分组后的key（相当于对Map输出的key做一个group             by）
         * @param values：从Map端输出并经过分组后的key                                对应的value的集合
         * @param context：MapReduce程序的上下文环境，Reduce端的输出可以通过context最终写到HDFS
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
            // hadoop,{1,1,1,1,1}
            long sum = 0;
            for (LongWritable value : values) {
                sum += value.get();
            }
            // 将最终结果输出
            context.write(key, new LongWritable(sum));
        }
    }

    // 组装MapReduce任务
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        // 配置reduce最终输出的key value的分隔符为 #
        conf.set("mapred.textoutputformat.separator", "#");
        // 做其他的配置

        // 创建一个Job
        Job job = Job.getInstance(conf);

        // 设置reduce的个数
        job.setNumReduceTasks(2);

        // 设置Job的名字
        job.setJobName("MyWordCountMapReduceApp");
        // 设置MapReduce运行的类
        job.setJarByClass(Demo2WordCount.class);

        // 配置Map
        // 配置Map Task运行的类
        job.setMapperClass(MyMapper.class);
        // 设置Map任务输出的key的类型
        job.setMapOutputKeyClass(Text.class);
        // 设置Map任务输出的value的类型
        job.setMapOutputValueClass(LongWritable.class);

        // 配置Reduce
        // 配置Reduce Task运行的类
        job.setReducerClass(MyReducer.class);
        // 设置Reduce任务输出的key的类型
        job.setOutputKeyClass(Text.class);
        // 设置Reduce任务输出的value的类型
        job.setOutputValueClass(LongWritable.class);
        job.setPartitionerClass(HashPartitioner.class);
        // 配置输入输出路径
        // 将输入的第一个参数作为输入路径，第二个参数作为输出的路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 提交任务并等待运行结束
        job.waitForCompletion(true);

    }
    /**
     * 打包并上传到master
     * 准备好输入的数据 并上传到HDFS的/input1
     *  hadoop jar jar包的路径 com.shujia.MapReduce.Demo2WordCount /input1 /output1
     */
}
